package org.zfes.snowier.zbss.xzy.face.uitl;


import javax.imageio.ImageIO;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.zbss.xzy.face.img.ImageInfo;

import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.DataBufferByte;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.Optional;

public class ImageUtil {

	public static Optional<ImageInfo> base64StringToImage(String base64String) {
	//	base64String=base64String.replace("data:image/jpeg;base64,", "");
		base64String=ZStrUtil.substringAfter(base64String, "base64,");
		 ByteArrayInputStream bais = null;
	    	try {
		    	byte[] bytes1 = Base64.getDecoder().decode(base64String);
		    	 bais = new ByteArrayInputStream(bytes1);
		    	BufferedImage bi1 = ImageIO.read(bais);
		    	
		    	Optional<ImageInfo> imageInfoOp = bufferedImage2ImageInfo(bi1);
		    	
		    	return imageInfoOp;
		    	
		    } catch (IOException e) {
		    	  e.printStackTrace();
		    	  return Optional.empty();
	    	}finally {
	    		try {
	    			if(bais!=null) {
	    				bais.close();
	    			}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	    	
	    }
	 
	 
	 
    public static Optional<ImageInfo> getRGBData(File file) {
        if (file == null)
            return Optional.empty();
        try {
            //将图片文件加载到内存缓冲区
            BufferedImage image = ImageIO.read(file);
            Optional<ImageInfo> imageInfoOp =bufferedImage2ImageInfo(image);
            return imageInfoOp;
        } catch (IOException e) {
            e.printStackTrace();
            return Optional.empty();
        }
    }

    public static  Optional<ImageInfo> getRGBData(InputStream input) {
        if (input == null)
        	 return Optional.empty();
        try {
            BufferedImage image = ImageIO.read(input);
            Optional<ImageInfo> imageInfoOp = bufferedImage2ImageInfo(image);
            return imageInfoOp;
        } catch (IOException e) {
        	e.printStackTrace();
        	 return Optional.empty();
        }finally {
            if(input!=null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static  Optional<ImageInfo> bufferedImage2ImageInfo(BufferedImage image){
        ImageInfo imageInfo = new ImageInfo();
        int width = image.getWidth();
        int height = image.getHeight();
        // 使图片居中
        width = width & (~3);
        height = height & (~3);
        imageInfo.width = width;
        imageInfo.height = height;
        //根据原图片信息新建一个图片缓冲区
        BufferedImage resultImage = new BufferedImage(width, height, image.getType());
        //得到原图的rgb像素矩阵
        int[] rgb = image.getRGB(0, 0, width, height, null, 0, width);
        //将像素矩阵 绘制到新的图片缓冲区中
        resultImage.setRGB(0, 0, width, height, rgb, 0, width);
        //进行数据格式化为可用数据
        BufferedImage dstImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        if(resultImage.getType() != BufferedImage.TYPE_3BYTE_BGR) {
            ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
            ColorConvertOp colorConvertOp = new ColorConvertOp(cs, dstImage.createGraphics().getRenderingHints());
            colorConvertOp.filter(resultImage, dstImage);
        } else {
            dstImage = resultImage;
        }

        //获取rgb数据
        imageInfo.rgbData = ((DataBufferByte) (dstImage.getRaster().getDataBuffer())).getData();
        return Optional.ofNullable(imageInfo);
    }

}
